package algorithm.kg.mpi_aida;


import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.squareup.okhttp.*;
import common.helper.toolkit.HttpToolkit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import com.fasterxml.jackson.databind.JsonNode;

//import common.helper.json.JsonToJavaObject;

/**
 * Created by julianzliu on 4/14/2017.
 */
public class AIDAClient {

    static final Logger logger = LoggerFactory.getLogger(AIDAClient.class);

    public static String entity_linking_unirest(String text){

        /*
        https://github.com/gammaliu/aida
         */
        /*
        Unirest 和 p00.common中引入的某些库冲突
         */
        HttpResponse<String> response = null;
        String body = "";
        String url = "https://gate.d5.mpi-inf.mpg.de/aida/service/disambiguate";
        //logger.info("[URL = ]\n{}" , url );
        System.out.println(String.format("[URL = ]\n%s" , url));
        //logger.info("[BODY = ]{}", body);
        System.out.println(String.format("[BODY = ]\n%s", body));
        try{
            body = "{\"text\" : \"" + text + "\"}";
            response = Unirest.post( url )
                    .header("content-type", "application/json")
                    .header("accept", "application/json")
                    .body(body)
                    .asString();
        }catch (UnirestException e){
            e.printStackTrace();
        }

        String response_str =  response.getBody();
        //JsonNode result = JsonToJavaObject.loadJsonNode(response_str);
        //return result;
        return response_str;
    }

    public static String entity_linking_okhttp(String text){
        /*
        https://developer.ambiverse.com/
        http://square.github.io/okhttp/
         */
        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        String body_text = "{\"text\" : \"" + text + "\"}";
        String url = "https://gate.d5.mpi-inf.mpg.de/aida/service/disambiguate";

        //logger.info("[URL = ]\n{}" , url );
        System.out.println(String.format("[URL = ]\n%s\n " , url));
        //logger.info("[BODY = ]{}", body);
        System.out.println(String.format("[BODY = ]\n%s\n", body_text));

        RequestBody body = RequestBody.create(mediaType, body_text);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("content-type", "application/json")
                .addHeader("accept", "application/json")
                //.addHeader("authorization", "Bearer ACCESS_TOKEN")
                .build();

        String result = null;
        try{
            Response response = client.newCall(request).execute();
            result = response.body().string();
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    public static String entity_linking(String text){
        String body_text = "{\"text\" : \"" + text + "\"}";
        String url = "https://gate.d5.mpi-inf.mpg.de/aida/service/disambiguate";

        //logger.info("[URL = ]\n{}" , url );
        System.out.println(String.format("[URL = ]\n%s\n " , url));
        //logger.info("[BODY = ]{}", body);
        System.out.println(String.format("[BODY = ]\n%s\n", body_text));

        String output = HttpToolkit.rest_post(url, body_text);
        return output;
    }


    /*****************************************************
     *
     * @param args
     */
    public static void main(String[] args){

        String result = AIDAClient.entity_linking("Jack founded Alibaba in Hangzhou with investments from SoftBank and Goldman.");

        //String pretty_result = JsonToJavaObject.jsonNodeToString(result);

        System.out.println(result);
    }

}
